package com.sojson.project.sys.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sojson.constant.ConstantByMap;
import com.sojson.project.sys.user.service.UserAdminService;
import com.sojson.result.Result;
import com.sojson.util.ExpUtil;
import com.sojson.util.ToolsUtil;
import com.sojson.util.file.FileUtil;
import com.sojson.util.servlet.ServletUtil;
import com.sojson.util.token.interfaces.BaseUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件控制器
 *
 * @author liu
 * @date 2021-03-18
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    private UserAdminService userAdminService;

    /**
     * 上传头像
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadHead")
    @PreAuthorize("@ss.login()")
    @ApiOperation("上传头像")
    public Result<String> avatar(MultipartFile file) throws Exception {
        // 上传文件
        Result<String> upload =
            ToolsUtil.upload(file, ConstantByMap.DEFAULT, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);

        BaseUserService userService = userAdminService.getUserService();

        // 修改数据
        userService.updateHeadImg(upload.getData());
        return upload;
    }

    /**
     * 下载文件
     *
     * @param objectName
     * @return
     * @throws Exception
     */
    @GetMapping("/download")
    @PreAuthorize("@ss.login()")
    public void downloadDelete(String objectName) throws Exception {
        if (!FileUtil.checkAllowDownload(objectName)) {
            ExpUtil.throwEx(String.format("文件名称({})非法，不允许下载。 ", objectName));
        }

        ServletUtil.outFile(FileUtil.downloadGetStream(objectName),
            objectName.substring(objectName.lastIndexOf("/") + 1));
    }

    /**
     * 上传企业资质照片
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadBusinessCert")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadBusinessCert(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.BUSINESS_CERT_IMG_PATH, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传商品分类缩略图
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadGoodCateThumb")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadGoodCateThumb(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.GOOD_CATE_THUMB, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传商品缩略图
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadGoodThumb")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadGoodThumb(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.GOOD_THUMB, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传商品详情轮播图
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadGoodImages")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadGoodImages(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.GOOD_IMAGES, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传商品规格明细缩略图
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadGoodSpecsAttrThumb")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadGoodSpecsAttrThumb(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.GOOD_SPECS_ATTR_THUMB, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传商品规格明细缩略图
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadComment")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadComment(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.COMMENT_IMG_PATH, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传头像
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadIdCard")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadIdCard(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.ID_CARD, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传广告图片
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadAd")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadAd(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.AD, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传许可证图片
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadBusinessLicense")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadBusinessLicense(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ToolsUtil.BUSINESS_LICENSE, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @PreAuthorize("@ss.login()")
    public Result<?> upload(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ConstantByMap.DEFAULT, ToolsUtil.IMG_VIDEO_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传图片
     *
     * @param files
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadList")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadList(@RequestParam MultipartFile[] file) throws Exception {
        return ToolsUtil.upload(file, ConstantByMap.DEFAULT, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

    /**
     * 上传推广海报背景图
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadBgPromotionPoster")
    @PreAuthorize("@ss.login()")
    public Result<?> uploadBgPromotionPoster(MultipartFile file) throws Exception {
        return ToolsUtil.upload(file, ConstantByMap.DEFAULT, ToolsUtil.IMG_WHITE_LIST, ToolsUtil.IMG_SIZE);
    }

}